import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue";
import Register from "../views/auth/Register.vue";
import NotFound from "../views/error/404.vue";
import Login from "../views/auth/Login.vue"
import Create from "../views/post/create.vue"
import Detail from "../views/post/Detail.vue"
import Search from "../views/search.vue"
import Profile from "../views/user/Profile.vue"
import Setting from "../views/user/Setting.vue"

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "Home",
    component: Home,
    meta: { title:"首页" }
  },
  {
    path: "/register",
    name: "Register",
    component: Register,
    meta: { title:"注册" }
  },
  {
    path: "/404",
    name: "404",
    component: NotFound,
    meta: { title:"错误" }
  },
  {
    path: "*",
    redirect: "/404",
    hidden: true
  },
  {
    name: "login",
    path: "/login",
    component: Login,
    meta: { title:"登陆" }
  },
  {
    name: "addPost",
    path: "/post/add",
    component: Create,
    meta: { title:"添加帖子" }
  },
  {
    name: "postDetail",
    path: "/post/detail/:id",
    component: Detail,
    meta: { title:"帖子详情" }
  },
  {
    name: "Search",
    path: "/post/search/:title",
    component: Search,
    meta: { title:"查询帖子" }
  },
  {
    name:"user-info",
    path: "/user/info/:id",
    component: Profile,
    meta: { title:"用户详情" }
  },
  {
    name: "user-edit",
    path: "/user/edit/:id",
    component: Setting,
    meta: { title:"用户编辑" }
  }
];
//为了让路由在首页跳转回首页的时候不报错
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location){
  return originalPush.call(this,location).catch(err => err);
}

const router = new VueRouter({
  // mode: "history",
  //注解掉不使用历史路由
  base: process.env.BASE_URL,
  routes
});

export default router;
